package com.asistencia.dao.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.asistencia.dao.interfaces.ClassroomDao;
import com.asistencia.domain.Career;
import com.asistencia.domain.Classroom;
import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
@Repository
@SuppressWarnings({ "rawtypes", "unchecked" })
public class ClassroomJdbc implements ClassroomDao{
	

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public List<Classroom> listAll() throws DataAccessException {
		String SQL="SELECT * FROM classroom";
		List matches=jdbcTemplate.query(SQL, new RowMapper(){
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Classroom clas= new Classroom();
				clas.setIdClassroom(rs.getInt("idclassroom"));
				clas.setName(rs.getString("name"));
				clas.setBlock(rs.getString("block"));
				return clas;
			}
			});
			
			return (matches.size() > 0 ? (List<Classroom>) matches : null);
	}
	@Override
	public List<Classroom> listByBlock(String block) throws DataAccessException {
		String SQL="SELECT * FROM classroom WHERE block = ?";
		List matches=jdbcTemplate.query(SQL, new Object[]{"%"+block+"%", "%"+block+"%"}, new RowMapper(){
		
			@Override
			public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Classroom clas=new Classroom();
				clas.setIdClassroom(rs.getInt("idclassroom"));
				clas.setName(rs.getString("name"));
				
			return clas;
			}
		});
		
		return (matches.size() > 0 ? (List<Classroom>) matches : null);
	}

	@Override
	public Classroom getById(Integer idClassroom) throws DataAccessException {
		String SQL="SELECT * FROM classroom WHERE idclassroom  =?";
		List matches =jdbcTemplate.query(SQL, new Object[] {idClassroom}, new RowMapper(){
			
@Override
	public Object mapRow(ResultSet rs, int rowNum) throws SQLException{
				Classroom classroom= new Classroom();
				classroom.setIdClassroom(rs.getInt("idclassroom"));
				classroom.setName(rs.getString("name"));
				classroom.setBlock(rs.getString("block"));
				return classroom;
}
});

return (matches.size() > 0 ? (Classroom) matches.get(0) : null);
}}
